package com.css.fxfzypg.modules.baseData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.dto.HouseDTO;
import com.css.fxfzypg.modules.baseData.queryParams.HouseQueryParams;
import com.css.fxfzypg.modules.baseData.entity.HouseEntity;
import com.css.fxfzypg.modules.baseData.service.HouseService;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-08-12 11:30
 * 房屋造价数据子模块
 */
@RestController
@RequestMapping("/house")
@Slf4j
public class HouseController {

    @Resource
    private HouseService houseService;

    /***
     * 查看
     * @param id
     * @return
     */
    @GetMapping("/getHouseById")
    public RestResponse getHouseById(String id){
        RestResponse restResponse = null;
        try{
            HouseEntity houseById = houseService.getHouseById(id);
            restResponse = RestResponse.succeed(houseById);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }


    /***
     * 修改
     * @param houseDTO
     * @return
     */
    @PostMapping("/updateHouse")
    public RestResponse updateHouse(@RequestBody HouseDTO houseDTO){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try {
            houseService.updateHouse(houseDTO);
            jsonObject.put("message","修改成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 分页查询
     * @param queryParams
     * @return
     */
    @GetMapping("/getAllHouse")
    public RestResponse getAllHouse(HttpServletRequest request, HouseQueryParams queryParams){
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=houseService.getAllHouseByParams(queryParams,curPage,pageSize);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 删除
     * @param
     * @return
     */
    @PostMapping("/deleteHouse/{ids}")
    public RestResponse deleteTask(@PathVariable("ids") String ids){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] arr= ids.split(",");
            List<String> list = Arrays.asList(arr);
            houseService.deleteHouse(list);
            jsonObject.put("message","删除完成");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }


    /***
     * 导入
     * @param file
     * @return
     */
    @PostMapping("/houseImportExcel")
    public RestResponse importExcel(MultipartFile file){
        RestResponse response = null;
        try {
            String info=houseService.importExcel(file);
            response = RestResponse.succeed(info);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }
}
